﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DosTexPrj.Core.Models;
using DosTexPrj.Core.RepositoriesInterface;
using DosTexPrj.Implement.NHibernate;
using DosTexPrj.UI.WinForm.Commons;

namespace DosTexPrj.UI.WinForm
{
    public partial class YarnOrderDetailForm : Form
    {
        #region --- Private members ---

        private IRepository _repository;

        #endregion --- Private members ---

        #region --- Properties ---

        public YarnOrderDetail CurrentOrderDetail { get; private set; }

        #endregion --- Properties ---

        #region --- Constructors ---

        public YarnOrderDetailForm()
        {
            InitializeComponent();
            _repository = MyServiceLocator.DoGetInstance<IRepository>();
            this.CurrentOrderDetail = null;
            this.txtWeight.KeyPress += new KeyPressEventHandler(Textbox_KeyPress);
            this.txtPrice.KeyPress += new KeyPressEventHandler(Textbox_KeyPress);
            this.txtQuantityOfBox.KeyPress += new KeyPressEventHandler(Textbox_KeyPress);
            this.txtPrice.MaxLength = this.txtWeight.MaxLength = 12;
            this.txtQuantityOfBox.MaxLength = 5;
        }

        private YarnOrderDetailForm(YarnOrderDetail orderDetailToEdit)
            : this()
        {
            this.CurrentOrderDetail = orderDetailToEdit;
        }

        #endregion --- Constructors ---

        private void YarnOrderDetailForm_Load(object sender, EventArgs e)
        {
            // Clear form and load comboboxes
            ClearForm();
        }

        private void Textbox_KeyPress(object sender, KeyPressEventArgs e)
        {
            Common.HandlingNumberTextbox(e);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValidate())
            {
                this.CurrentOrderDetail = null;
                return;
            }

            this.CurrentOrderDetail = new YarnOrderDetail();
            this.CurrentOrderDetail.Composition = (Composition)cboComposition.SelectedItem;

            int quantityOfBox = 0;
            int.TryParse(txtQuantityOfBox.Text.Trim(), out quantityOfBox);
            this.CurrentOrderDetail.QuantityOfBox = quantityOfBox;

            double weight = 0;
            double.TryParse(txtWeight.Text.Trim(), out weight);
            this.CurrentOrderDetail.Weigth = weight;

            double price = 0;
            double.TryParse(txtPrice.Text.Trim(), out price);
            this.CurrentOrderDetail.Price = price;

            this.Close();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearForm();
        }

        #region Functions

        private void LoadComposition()
        {
            try
            {
                var compositionData = _repository.GetAll<Composition>();
                IList<Composition> compositions = new List<Composition>();

                compositions = (from c in compositionData
                                select new Composition
                                {
                                    Id = c.Id,
                                    CompostionName = c.CompostionName
                                }).ToList();

                if (compositions.Count > 0)
                {
                    cboComposition.DataSource = compositions;
                    cboComposition.DisplayMember = "CompostionName";
                    cboComposition.ValueMember = "Id";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void ClearForm()
        {
            // Clear textboxes
            txtWeight.Text = string.Empty;
            txtPrice.Text = string.Empty;
            txtQuantityOfBox.Text = string.Empty;
            // Load comboboxes
            LoadComposition();
        }

        private bool CheckValidate()
        {
            error.Clear();
            bool flag = true;
            if (!Common.IsBlankTextBox(txtWeight, "Required field!", error))
                flag = false;
            if (!Common.IsBlankTextBox(txtPrice, "Required field!", error))
                flag = false;
            if (!Common.IsBlankTextBox(txtQuantityOfBox, "Required field!", error))
                flag = false;
            return flag;
        }

        #endregion Functions
    }
}